package com.wl.dzt.sys.controller.result.async;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.Callable;

/**
 * @author wl_code@163.com
 * @version 1.0
 * @date 2018/7/5/005
 * @description 异步请求
 */
public class AsyncRequest {
    private HttpServletResponse response;
    private PrintWriter writer;

    public AsyncRequest(HttpServletResponse response) {
        this.response = response;
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void print(String html) {
        writer.print(html);
        writer.flush();
    }

    public Callable<ModelAndView> start(AsyncRequestExcuter asyncRequestExcuter) {
        return () -> {
            asyncRequestExcuter.run(this);
            writer.close();
            return null;
        };
    }
}
